<?php

namespace App\Http\Controllers\V1\API;

use App\Models\V1\Colleges;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;

class CollegesController extends BaseController
{
    /****
     * 校区列表接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function collegesData(Request $request) {
        $result = array(
            'errorCode' => Response::HTTP_NOT_FOUND,
            'errorMsg' => Response::$statusTexts[Response::HTTP_NOT_FOUND],
            'data' => array()
        );
        $collegeList = $this->getCollegesList();
        if ($collegeList->isEmpty()) {
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_NO_CONTENT];
        }else {
            // 格式化数据
            $colleges = array();
            foreach ($collegeList as $item) {
                $formatCollege = array();
                $formatCollege['collegeCode'] = $item['code'];
                $formatCollege['collegeName'] = $item['name'];
                $formatCollege['address'] = isset($item['address'])?$item['address']:'';
                $formatCollege['longitude'] = $item['longitude'];
                $formatCollege['latitude'] = $item['latitude'];
                array_push($colleges, $formatCollege);
            }
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_OK];
            $result['data'] = $colleges;
        }
        return $this->sendResponse($result, $result['errorCode']);
    }

    /****
     * 获取所有校区
     * @return \Illuminate\Support\Collection
     */
    private function getCollegesList()
    {
        $data = Colleges::orderBy('id','asc')->get();
        return $data;
    }
}
